package com.cyw.note.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyw.common.core.domain.PageQuery;
import com.cyw.common.core.domain.R;
import com.cyw.common.core.page.TableDataInfo;
import com.cyw.common.utils.StringUtils;
import com.cyw.note.manage.domain.Links;
import com.cyw.note.manage.domain.bo.LinksBo;
import com.cyw.note.manage.domain.vo.LinksVo;
import com.cyw.note.manage.mapper.LinksMapper;
import com.cyw.note.manage.service.ILinksService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 友情链接Service业务层处理
 *
 * @author Chenyw
 * @date 2022-08-07
 */
@RequiredArgsConstructor
@Service
public class LinksServiceImpl implements ILinksService {

    private final LinksMapper baseMapper;

    @Override
    public R queryAll() {
        return R.ok(baseMapper.selectList());
    }

    /**
     * 查询友情链接
     */
    @Override
    public LinksVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询友情链接列表
     */
    @Override
    public TableDataInfo<LinksVo> queryPageList(LinksBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Links> lqw = buildQueryWrapper(bo);
        Page<LinksVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询友情链接列表
     */
    @Override
    public List<LinksVo> queryList(LinksBo bo) {
        LambdaQueryWrapper<Links> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Links> buildQueryWrapper(LinksBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Links> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBlogAddress()), Links::getBlogAddress, bo.getBlogAddress());
        lqw.like(StringUtils.isNotBlank(bo.getBlogName()), Links::getBlogName, bo.getBlogName());
        lqw.eq(StringUtils.isNotBlank(bo.getPictureAddress()), Links::getPictureAddress, bo.getPictureAddress());
        return lqw;
    }

    /**
     * 新增友情链接
     */
    @Override
    public Boolean insertByBo(LinksBo bo) {
        Links add = BeanUtil.toBean(bo, Links.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改友情链接
     */
    @Override
    public Boolean updateByBo(LinksBo bo) {
        Links update = BeanUtil.toBean(bo, Links.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Links entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除友情链接
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
